import Vue from 'vue'
import Router from 'vue-router'
import store from './store'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css'// Progress 进度条样式
import { getUserInfo } from './api/bitahub'
Vue.use(Router)
const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      component: () => import('@/components/index/landing_index'),
      children: [
        {
          path: '/',
          name: 'landing',
          component: () => import('@/components/page/loading_index_content')
        },
        {
          path: '/landingprice',
          name: 'landingprice',
          component: () => import('@/components/page/loading_index_price')
        },
        {
          path: '/chargingtips',
          name: 'chargingtips',
          component: () => import('@/components/login/charging_tips.vue')
        }
      ]
    },
    {
      path: '/index',
      component: () => import('@/components/index/index'),
      children: [
        {
          path: '/',
          name: 'home',
          meta: { requireAuth: true },
          component: () => import('@/components/home/home')
        },
        {
          path: '/newmirror',
          name: 'newMirror',
          meta: { requireAuth: true },
          component: () => import('@/components/mirroring/new_mirror')
        },
        {
          path: '/mirrordetails',
          name: 'mirrorDetailsr',
          meta: { requireAuth: true },
          component: () => import('@/components/mirroring/mirror_details')
        },
        {
          path: '/personalmirror',
          name: 'personalMirro',
          meta: { requireAuth: true },
          component: () => import('@/components/mirroring/personal_mirro')
        },
        {
          path: '/teamreset',
          name: 'teamReset',
          meta: { requireAuth: true },
          component: () => import('@/components/team/team_reset')
        },
        {
          path: '/jointeam',
          name: 'joinTeam',
          component: () => import('@/components/team/join_team')
        },
        {
          path: '/teamadd',
          name: 'teamAdd',
          meta: { requireAuth: true },
          component: () => import('@/components/team/team_add')
        },
        {
          path: '/project',
          name: 'project',
          meta: { requireAuth: true, title: '项目-Bitahub' },
          component: () => import('@/components/project/index'),
          children: [
            {
              path: '/project/list',
              name: 'projectList',
              meta: { requireAuth: true },
              component: () => import('@/components/project/project_list')
            },
            {
              path: '/project/task',
              name: 'projectTask',
              meta: { requireAuth: true },
              component: () => import('@/components/project/project_task')
            }
          ]
        },
        {
          path: '/project/quickstart',
          name: 'projectQuickStart',
          meta: { requireAuth: true },
          component: () => import('@/components/project/quick_start')
        },
        {
          path: '/project/create',
          name: 'projectCreate',
          meta: { requireAuth: true },
          component: () => import('@/components/project/project_create')
        },
        {
          path: '/project/run',
          name: 'projectRun',
          meta: { requireAuth: true },
          component: () => import('@/components/project/project_run')
        },
        {
          path: '/project/edit',
          name: 'projectEdit',
          meta: { requireAuth: true },
          component: () => import('@/components/project/project_edit')
        },
        {
          path: '/project/detail',
          name: 'projectDetail',
          meta: { requireAuth: true },
          component: () => import('@/components/project/project_detail'),
          children: [
            {
              path: '/project/detail/task',
              name: 'projectDetailTask',
              meta: { requireAuth: true },
              component: () => import('@/components/project/project_task')
            },
            {
              path: '/project/detail/code',
              name: 'projectCode',
              meta: { requireAuth: true },
              component: () => import('@/components/project/project_code')
            },
            {
              path: '/project/detail/dataset',
              name: 'projectDataSet',
              meta: { requireAuth: true },
              component: () => import('@/components/project/project_dataset')
            },
            {
              path: '/project/detail/notebook',
              name: 'projectNotebook',
              meta: { requireAuth: true },
              component: () => import('@/components/project/project_notebook')
            }
          ]
        },
        {
          path: '/project/task/view',
          name: 'projectTaskView',
          meta: { requireAuth: true },
          component: () => import('@/components/project/task_view'),
          children: [
            {
              path: '/project/task/view/result',
              name: 'projectTaskViewResult',
              meta: { requireAuth: true },
              component: () => import('@/components/project/task_view_result')
            },
            {
              path: '/project/task/view/chart',
              name: 'projectTaskViewChart',
              meta: { requireAuth: true },
              component: () => import('@/components/project/task_view_chart')
            },
            {
              path: '/project/task/view/output',
              name: 'projectTaskViewOutput',
              meta: { requireAuth: true },
              component: () => import('@/components/project/task_view_output')
            },
            {
              path: '/project/task/view/snapshot',
              name: 'projectTaskViewSnapshot',
              meta: { requireAuth: true },
              component: () => import('@/components/project/task_view_snapshot')
            }
          ]
        },
        {
          path: '/project/open',
          name: 'projectOpen',
          meta: { requireAuth: true },
          component: () => import('@/components/project/project_open')
        },
        {
          path: '/filecenter',
          name: 'filecenter',
          meta: { requireAuth: true },
          component: () => import('@/components/file_center/index')
        },
        {
          path: '/filecenter/editor',
          name: 'editor',
          meta: { requireAuth: true },
          component: () => import('@/components/file_center/editor')
        },
        // {
        //   path: '/filemigration',
        //   name: 'filemigration',
        //   meta: { requireAuth: true },
        //   component: () => import('@/components/file_center/file_migration')
        // },
        {
          path: '/dataset',
          name: 'dataset',
          meta: { requireAuth: true },
          component: () => import('@/components/dataset/dataset')
        },
        {
          path: '/dataset/create',
          name: 'datasetCreate',
          meta: { requireAuth: true },
          component: () => import('@/components/dataset/dataset_create')
        },
        {
          path: '/dataset/detail',
          name: 'datasetDetail',
          meta: { requireAuth: true },
          component: () => import('@/components/dataset/dataset_detail')
        },
        {
          path: '/modellump',
          name: 'modellump',
          meta: { requireAuth: true },
          component: () => import('@/components/modellump/modellump')
        },
        {
          path: '/modellump/modelnew',
          name: 'modelnew',
          meta: { requireAuth: true },
          component: () => import('@/components/modellump/model_new')
        },
        {
          path: '/modellump/modelopen',
          name: 'modelopen',
          meta: { requireAuth: true },
          component: () => import('@/components/modellump/model_open')
        },
        {
          path: '/modellump/privatedetailed',
          name: 'privatedetailed',
          meta: { requireAuth: true },
          component: () => import('@/components/modellump/model_private_detailed')
        },
        {
          path: '/modellump/opendetailed',
          name: 'opendetailed',
          meta: { requireAuth: true },
          component: () => import('@/components/modellump/model_open_detailed')
        },
        {
          path: '/openproject',
          name: 'openProject',
          meta: { requireAuth: true },
          component: () => import('@/components/public_project/openproject')
        },
        {
          path: '/openproject/detail',
          name: 'openProjectDetail',
          meta: { requireAuth: true },
          component: () => import('@/components/public_project/openproject_detail')
        },
        {
          path: '/openproject/task/view',
          name: 'openprojectTaskView',
          meta: { requireAuth: true },
          component: () => import('@/components/public_project/task_view'),
          children: [
            {
              path: '/openproject/task/view/result',
              name: 'openprojectTaskViewResult',
              meta: { requireAuth: true },
              component: () => import('@/components/public_project/task_view_result')
            },
            {
              path: '/openproject/task/view/chart',
              name: 'openprojectTaskViewChart',
              meta: { requireAuth: true },
              component: () => import('@/components/public_project/task_view_chart')
            },
            {
              path: '/openproject/task/view/output',
              name: 'openprojectTaskViewOutput',
              meta: { requireAuth: true },
              component: () => import('@/components/public_project/task_view_output')
            },
            {
              path: '/openproject/task/view/snapshot',
              name: 'openprojectTaskViewSnapshot',
              meta: { requireAuth: true },
              component: () => import('@/components/public_project/task_view_snapshot')
            }
          ]
        },
        {
          path: '/search/detail',
          name: 'searchDetail',
          meta: { requireAuth: true },
          component: () => import('@/components/search/search_detail')
        },
        {
          path: '/feedback',
          name: 'feedback',
          meta: { requireAuth: true },
          component: () => import('@/components/feedback/index')
        },
        {
          path: '/order',
          name: 'order',
          meta: { requireAuth: true },
          component: () => import('@/components/order/order_package.vue')
        },
        {
          path: '/order/list',
          name: 'orderLask',
          meta: { requireAuth: true },
          component: () => import('@/components/order/order_machine_list.vue')
        },
        {
          path: '/pay/success',
          name: 'paySuccess',
          meta: { requireAuth: true },
          component: () => import('@/components/order/pay_success.vue')
        },
        {
          path: '/pay/fail',
          name: 'payFail',
          meta: { requireAuth: true },
          component: () => import('@/components/order/pay_fail.vue')
        },
        {
          path: '/team/detail',
          name: 'team',
          meta: { requireAuth: true },
          component: () => import('@/components/team/team_detail'),
          children: [
            {
              path: '/team/detail/member',
              name: 'teamMember',
              meta: { requireAuth: true },
              component: () => import('@/components/team/team_member')
            },
            {
              path: '/team/detail/count',
              name: 'teamCount',
              meta: { requireAuth: true },
              component: () => import('@/components/team/team_count')
            }
          ]
        }
      ]
    },
    {
      path: '/jupyter',
      name: 'jupyter',
      component: () => import('@/components/jupyter/index')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/components/login/login')
    },
    {
      path: '/loginout',
      name: 'loginout',
      component: () => import('@/components/login/loginout')
    }
  ]
})

router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  NProgress.start()
  // 处理携带token、bitaToken直接进入登录状态问题
  if (to.query && to.query.bitaToken && to.query.token) {
    let userInfo = {
      token: to.query.token,
      bitaToken: to.query.bitaToken
    }
    const query = `?token=${to.query.bitaToken}`
    getUserInfo(query).then(res => {
      if (res.code === 0) {
        userInfo = { ...res.result, ...userInfo }
        store.commit('SET_USERINFO', userInfo)
      } else {
        this.$message({ message: res.msg, showClose: true, type: 'error' })
      }
    })
    next({
      path: '/'
    })
  } else if (to.matched.some(r => r.meta.requireAuth)) {
    if (store.state.userInfo.token && store.state.userInfo.bitaToken) {
      next()
    } else {
      next({
        path: '/login'
      })
    }
  } else {
    next()
  }
})

router.afterEach(() => {
  NProgress.done() // 结束Progress
})

export default router
